

package metadatabrowser;

import java.util.List;
import at.fhj.itm.metadata.Column;
import at.fhj.itm.metadata.Constraint;
import at.fhj.itm.metadata.Index;
import at.fhj.itm.metadata.MetadataItem;
import at.fhj.itm.metadata.Procedure;
import at.fhj.itm.metadata.Schema;
import at.fhj.itm.metadata.Sequence;
import at.fhj.itm.metadata.Table;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;


/**
 *
 * @author Seuchter
 */
public class SchemaTreeContentProvider implements ITreeContentProvider {


    private List<? extends Schema> schemas;


    public SchemaTreeContentProvider(final List<? extends Schema> schemas)
    {
        this.schemas = schemas;
    }

    @Override
    public Object[] getElements(Object o)
    {
        return schemas.toArray();
    }

    @Override
    public Object[] getChildren(Object o)
    {

        if(o instanceof Schema)
        {
            Schema s = ((Schema) o);

            List<? extends Table> t = s.getTables();

            MetadataWrapper<? extends Table> tables  = new MetadataWrapper<Table>(Table.class, (List<Table>) t);
            MetadataWrapper<? extends Sequence> seq = new MetadataWrapper<Sequence>(Sequence.class, (List<Sequence>)s.getSequences());
            MetadataWrapper<? extends Procedure> procs = new MetadataWrapper<Procedure>(Procedure.class, (List<Procedure>) s.getProcedures());
            return new Object[]{seq,tables,procs};
        }
        else if(o instanceof MetadataWrapper)
        {
            MetadataWrapper<MetadataItem> items = (MetadataWrapper<MetadataItem>) o;

            //if(items.getItemClass().isAssignableFrom(Table.class))
            {
                return items.getList().toArray();
            }
        }
        else if(o instanceof  Table)
        {
            Table t = ((Table) o);

            MetadataWrapper<Column> col = new MetadataWrapper<Column>(Column.class, (List<Column>) t.getColumns());
            MetadataWrapper<Index> ind = new MetadataWrapper<Index>(Index.class, (List<Index>)t.getIndices());
            MetadataWrapper<Constraint> con = new MetadataWrapper<Constraint>(Constraint.class, (List<Constraint>) t.getConstraints());

            return new Object[]{col, ind, con};
        }

        return null;
    }

    @Override
    public Object getParent(Object o)
    {
        return null;
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean hasChildren(Object o)
    {
        if(o instanceof Schema)
        {
            return ((Schema) o).getTables().size() > 0;
        }
        if(o instanceof MetadataWrapper)
        {
            
			MetadataWrapper<MetadataItem> items =
                    (MetadataWrapper<MetadataItem>) o;

             return items.getList().size() > 0;
        }
        if(o instanceof Table)
        {
            return ((Table) o).getChildren().size() > 0;
        }

        return false;

    }

    @Override
    public void dispose()
    {
       
    }

    @Override
    public void inputChanged(Viewer viewer, Object o, Object o1)
    {
        
    }

}
